#include <stdio.h>
#include <stdarg.h>
#include "file_config.h"
#include "hj_log.h"

static int file_config_test(void);

int main(int argc, char *argv[])
{
    LOG_INFO("file config example is starting...");

    int ret = file_config_test();
    LOG_INFO("test result: %d", ret);

    return 0;
}

static int file_config_test(void)
{
    fconf_t fconf;
    fconf_t *pfc = &fconf;
    char file_name[] = "../user_config.conf";
    int ret = 0;

    ret = fconf_init(&fconf, file_name);
    if (ret)
    {
        LOG_ERROR("ret: %d", ret);
        return ret;
    }

    ret = pfc->print(pfc);
    if (ret)
    {
        LOG_INFO("ret: %d", ret);
        return ret;
    }

    ret = pfc->add(pfc, "data", "20210430");
    if (ret)
    {
        LOG_INFO("ret: %d", ret);
        return ret;
    }

    ret = pfc->print(pfc);
    if (ret)
    {
        LOG_INFO("ret: %d", ret);
        return ret;
    }

    char *param = NULL;
    ret = pfc->get(pfc, "file_path", &param);
    if (ret)
    {
        LOG_INFO("ret: %d", ret);
        return ret;
    }
    else
    {
        LOG_INFO("[name = %s]\n[param = %s]", "file_path", param);
    }

    ret = pfc->remove(pfc, "test");
    if (ret)
    {
        LOG_INFO("ret: %d", ret);
        return ret;
    }

    int size = pfc->get_size(pfc);
    LOG_INFO("File config size: %d", size);

    ret = pfc->save(pfc, "../user_config_backup.conf");
    if (ret)
    {
        LOG_INFO("ret: %d", ret);
        return ret;
    }

    return 0;
}